package com.day01;

import java.util.Arrays;
import java.util.Random;

/**
 * 2022年4月3日 14:20:01
 */
public class 冒泡排序01 {
    public static void bubbleSort(int arr[]) {
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j] < arr[i]) {
                    swap(arr, i, j);
                }
            }
        }
    }

    public static void swap(int arr[], int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    public static void main(String[] args) {
        Random random = new Random();
        int arr[] = new int[100];
        int arr1[] = new int[100];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = random.nextInt(1000);
        }
        System.arraycopy(arr, 0, arr1, 0, arr.length);
        Arrays.sort(arr1);
        bubbleSort(arr);
        boolean flag = true;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == arr1[i]) {
                flag = flag && true;
            } else {
                flag = false;
            }
        }
        System.out.println("冒泡排序结果是否正确: " + (flag ? "√" : "×"));
    }
}
